// JavaScript Document
var zoomOpen = false;
var objCurrent = null;
var tmpHeightImageZoom;
var tmpWidthImageZoom;
var currentGroupImage = "";
var indexImgCliccataGroup = 0;
$(document).ready(function(){

	$(".img_appoggio_zoom_product").load(function(){
		openZoomProduct();
	});
	
	$(window).resize(function(){
		if($(".wrapper_zoom_product").css('display') != "none")
		posizionaZoomProduct(true);

		if($('img[group="'+currentGroupImage+'"]').eq(indexImgCliccataGroup).attr('video') != "")
			setDimensionVideo(true);

	});
	
	$('.bt_zoom').on('click',function(){
		
		//dataLayer.push({"event": "zoom"});
		writeImage($(this).parent().find('img'))
		
		currentGroupImage = $(this).parent().find('img').attr('group');	
		var tmpValueSrcImage = $(this).parent().find('img').attr('src')
		
		for(i=0; i<$('img[group="'+currentGroupImage+'"]').length; i++)
		{
			if($('img[group="'+currentGroupImage+'"]').eq(i).attr('src') == tmpValueSrcImage)
				indexImgCliccataGroup = i;
		}
		
		checkArrowZoom($(this).parent().find('img'));
	});
	
	/*
	$('.zoom').on('click',function(){
		alert('a');
		writeImage($(this))
	});
	*/
	
});

$('.zoom').live('click',function(){
	
	dataLayer.push({"event": "zoom"});
	writeImage($(this))
	//indexImgCliccataGroup
	
	currentGroupImage = $(this).attr('group');
	var tmpValueSrcImage = $(this).attr('src')
	for(i=0; i<$('img[group="'+currentGroupImage+'"]').length; i++)
	{
		if($('img[group="'+currentGroupImage+'"]').eq(i).attr('src') == tmpValueSrcImage)
			indexImgCliccataGroup = i;
	}
	 
	checkArrowZoom($(this));
	
});

function writeImage(obj)
{	
	objCurrent = obj;
	if(objCurrent.attr('video') != "" && objCurrent.attr('video') != undefined )
	{
		playVideoZoom(objCurrent.attr('video'));
		return;
	}
		
	var image = "";
	var valueSrcImage = obj.attr('src');
	if(obj.attr('alias') != undefined && obj.attr('alias') != "" )
	{
		valueSrcImage = obj.attr('alias');
	}
	
	if((parseInt(objCurrent.attr('resize'),10) == 0 || isNaN(parseInt(objCurrent.attr('resize'),10))) && ( parseInt(objCurrent.attr('forceExtension'),10) == 1 || isNaN(parseInt(objCurrent.attr('forceExtension'),10))) )
	{
		if(obj.attr('src') == "" || obj.attr('src') == undefined)
		{
			image = obj.parent().attr('img').replace('thumb','zoom').replace('png','jpg');
			valueSrcImage = obj.parent().attr('img');
			//obj = obj.parent().attr('.img');
		}
		else
		{
			image = valueSrcImage.replace('thumb','zoom').replace('png','jpg');
		}
		
	}
	else
	{
		if(obj.attr('src') == "" || obj.attr('src') == undefined)
		{
			image = obj.parent().attr('img').replace('thumb','zoom');
			valueSrcImage = obj.parent().attr('img');
		}
		else
		{
			image = valueSrcImage.replace('thumb','zoom');
		}
	}
	
	if(valueSrcImage.indexOf('thumb') == -1)
	{
		
		var tmpArrPathImage = valueSrcImage.split('/');
		image = "";
		for(i=0; i<tmpArrPathImage.length; i++)
		{
			if(i == tmpArrPathImage.length-1)
			{
				image += 'zoom/'+tmpArrPathImage[i];
			}
			else
			{
				image += tmpArrPathImage[i]+'/';
			}
		}
		//image += 'http://';
		//alert(image)
	}	
	
	$('.loader_zoom_prodotti').fadeIn();
	$(".wrapper_zoom_product").css({width: window_width+"px", height: window_height+"px"}).fadeIn();
	$('.img_appoggio_zoom_product').attr('src',image);
}

function openZoomProduct()
{
	if($('.img_appoggio_zoom_product').attr('src') == undefined)
	return;
	if($('.img_appoggio_zoom_product').attr('src').indexOf('spacer') != -1)
	return;
	
	zoomOpen=true;
	tmpHeightImageZoom = parseInt($('.img_appoggio_zoom_product').height(),10);
	tmpWidthImageZoom = parseInt($('.img_appoggio_zoom_product').width(),10);
	
	$(".wrapper_zoom_product").css({width: window_width+"px", height: (parseInt($('#section_template').height(),10)+HEIGHT_FOOTER)+"px"}).fadeIn();
	$(".wrapper_zoom_product").find('img.image_zoom_product').hide().css('visibility','visible').attr('src',$('.img_appoggio_zoom_product').attr('src')).fadeIn();
	
	//$(".wrapper_zoom_product").css({width: window_width+"px", height: window_height+"px"}).fadeIn();
	$(".wrapper_zoom_product").find('.content_cnt_desc_prod_zoom .desc_prod_zoom').html($('.cnt_col_dx_dati .specifiche').html());
	$(".wrapper_zoom_product").find('.content_cnt_desc_prod_zoom .nome_prod_zoom').html($('.title_argomento_scheda').html());
	
	//$(".wrapper_zoom_product").find('.content_cnt_desc_prod_zoom').html($('.cnt_col_dx_dati .specifiche').html());
	
	
	
	//trackEventAnalytics('Zoom-product', 'Click', window.location.hash.replace("#",""));
	
	posizionaZoomProduct(false);
}

function posizionaZoomProduct(fromResize)
{
	if($(".wrapper_zoom_product").find('img.image_zoom_product').hasClass('ui-draggable'))
	$(".wrapper_zoom_product").find('img.image_zoom_product').removeClass('ui-draggable');
	
	$('.loader_zoom_prodotti').fadeOut();
	
	var tmpWindowWhidth = window_width;
	var tmpWindowheight = window_height;
	
	if(window_height < MIN_HEIGHT)
	{	
		tmpWindowheight = MIN_HEIGHT;
	}
	
	if(window_width < MIN_WIDTH)
	{
		tmpWindowWhidth = MIN_WIDTH
	}
		
	if(parseInt(objCurrent.attr('resize'),10) == 1)
	{
		if(tmpHeightImageZoom > tmpWidthImageZoom)
		{
			var newheightImage = window_height;
			var newWidthImage = Math.floor((tmpWidthImageZoom*window_height)/tmpHeightImageZoom);
		}
		else
		{
			var newWidthImage = window_width;
			var newheightImage = Math.floor((tmpHeightImageZoom*window_width)/tmpWidthImageZoom);
		}
		
		var h = tmpWindowheight  + ( (newheightImage - tmpWindowheight ) * 2);
		$(".cnt_drag_image").css({width: (newWidthImage-2)+"px", height: newheightImage+'px', top: '0px', left: '0px', overflow: 'visible'});
		$(".cnt_drag_image .marginZoom").css({marginLeft: "-1px"});
		
		var top = (window_height - newheightImage);	
		
		$(".wrapper_zoom_product").find('.marginZoom').css({marginTop: (top/2)+'px'});
		$(".wrapper_zoom_product").find('img.image_zoom_product').addClass('nocursor');
		$(".wrapper_zoom_product").find('img.image_zoom_product').width(newWidthImage).height(newheightImage);	
		//$(".wrapper_zoom_product").find('img.image_zoom_product').draggable({ axis: "y", containment: '.cnt_drag_image', scroll: false});
		
		if(newWidthImage < window_width)
		{
			$(".wrapper_zoom_product").find('.marginZoom').css({marginLeft: ((window_width-newWidthImage)/2)+'px'});
		}
		
	}
	else
	{
		$(".wrapper_zoom_product").find('.marginZoom').css({marginLeft: '0px'});
		$(".wrapper_zoom_product").find('img.image_zoom_product').css('top','0px')
		var h = tmpWindowheight  + ( (tmpHeightImageZoom - tmpWindowheight ) * 2);
		var left = (tmpWindowWhidth - tmpWidthImageZoom) / 2;
		var top = ($('.img_appoggio_zoom_product').height() - window_height);	
		if($('.img_appoggio_zoom_product').height() < window_height)
			top = (window_height - $('.img_appoggio_zoom_product').height());	
		
		$(".wrapper_zoom_product").find('img.image_zoom_product').width(tmpWidthImageZoom).height(tmpHeightImageZoom);	
		
		if($('.img_appoggio_zoom_product').height() < window_height)
		{
			$(".cnt_drag_image").css({width: (tmpWidthImageZoom-2)+"px", height: h+'px', left: left+'px', top: 'auto', overflow: 'visible'});
			$(".cnt_drag_image .marginZoom").css({marginLeft: "-1px"});
			$(".wrapper_zoom_product .image_zoom_product").css('cursor','default');
		}
		else
		{
			$(".cnt_drag_image").css({width: (tmpWidthImageZoom-2)+"px", height: h+'px', top: "-"+top+'px', left: left+'px'});
			$(".cnt_drag_image .marginZoom").css({marginLeft: "-1px"});
		}
		
		$(".wrapper_zoom_product").find('.marginZoom').css({marginTop: "+"+(top/2)+'px'});
		$(".wrapper_zoom_product").find('img.image_zoom_product').removeClass('nocursor');
		
		if($('.img_appoggio_zoom_product').height() > window_height)
			$(".wrapper_zoom_product").find('img.image_zoom_product').draggable({ axis: "y", containment: '.cnt_drag_image', scroll: false});
	}	
	
	if(fromResize)
		$(".wrapper_zoom_product").css({width: tmpWindowWhidth+"px", height: tmpWindowheight+"px"}).fadeIn();
}

function closeZoomProduct()
{
	$(".wrapper_zoom_product").fadeOut(function(){
		$(".wrapper_zoom_product").find('img.image_zoom_product').attr('src','images/spacer.gif').removeAttr('src')
		$('.img_appoggio_zoom_product').attr('src','images/spacer.gif').removeAttr('src');	
		if($('#video_zoom').html() != "")
		{
			$('.wrap_video_zoom').css({zIndex: '0'});
			$('.wrap_video_zoom').html('<div id="video_zoom"></div>');
		}
	});
	objCurrent = null;
	zoomOpen=false;
}

function checkArrowZoom(obj)
{
	if(obj.attr('group') != '' && obj.attr('group') != undefined)
	{
		currentGroupImage = obj.attr('group');
		var nImgGroup = parseInt($('img[group="'+obj.attr('group')+'"]').length,10);
		
		if(nImgGroup > 1)
		{
			$('.arrow_zoom').css('display','inline');
			//indexImgCliccataGroup = parseInt(obj.parent().index(),10);
			
			//gestisco visibilità frecce a seconda della posizione
			if(indexImgCliccataGroup == 0)
			{
				$('.arrow_zoom.sx').css('display','none');
			}
			else if(indexImgCliccataGroup == nImgGroup-1)
			{
				$('.arrow_zoom.dx').css('display','none');
			}
		}
		else
		{
			$('.arrow_zoom').css('display','none');
		}
	}
	else
	{
		$('.arrow_zoom').css('display','none');
	}
}

function goZoomByArrow(direction)
{
	if(direction)
	{
		indexImgCliccataGroup++;
		
		if($('img[group="'+currentGroupImage+'"]').eq(indexImgCliccataGroup-1).attr('video') != "" && $('img[group="'+currentGroupImage+'"]').eq(indexImgCliccataGroup-1).attr('video') != undefined)
		{
			$('.wrap_video_zoom').html('<div id="video_zoom"></div>');
		}
		//alert(indexImgCliccataGroup + " " + direction)
	}
	else
	{
		indexImgCliccataGroup--;
		
		if($('img[group="'+currentGroupImage+'"]').eq(indexImgCliccataGroup+1).attr('video') != "" && $('img[group="'+currentGroupImage+'"]').eq(indexImgCliccataGroup+1).attr('video') != undefined)
		{
			$('.wrap_video_zoom').html('<div id="video_zoom"></div>');
		}
	}
	
	writeImage($('img[group="'+currentGroupImage+'"]').eq(indexImgCliccataGroup));
	checkArrowZoom($('img[group="'+currentGroupImage+'"]').eq(indexImgCliccataGroup));
	
	//writeImage($('img[group="'+currentGroupImage+'"]').parent().eq(indexImgCliccataGroup).find('img.zoom'));
	//checkArrowZoom($('img[group="'+currentGroupImage+'"]').parent().eq(indexImgCliccataGroup).find('img.zoom'));
}
var newWidthVideo = 960;
var newHeightVideo = 540;
var videoInPlay = false;
function setDimensionVideo(fromResize)
{
	if(fromResize == undefined || fromResize == null)
		fromResize = false;

	var contetnHeight = window_height;
	newWidthVideo = window_width;
	newHeightVideo = (newWidthVideo*9)/16;

	var marginTopVideo = (window_height-newHeightVideo)/2;
	var marginLeftVideo = (window_width-newWidthVideo)/2;

	if(newHeightVideo > window_height)
	{
		newHeightVideo = window_height;
		newWidthVideo = (newHeightVideo*16)/9;
		marginLeftVideo = (window_width-newWidthVideo)/2;
		marginTopVideo = (contetnHeight-newHeightVideo)/2;
	}

	$(".wrap_video_zoom").width(newWidthVideo).height(newHeightVideo).css('top',marginTopVideo+'px').css('left',marginLeftVideo+'px').css('margin-left','auto').css('margin-top','auto');

	if(videoInPlay && fromResize && document.getElementById('video_zoom') != null && document.getElementById('video_zoom') != "undefined")
	{
		document.getElementById('video_zoom').pauseMediaElementToJS();		
		$('#video_zoom').css({width: newWidthVideo+'px',height: newHeightVideo+'px'});
		document.getElementById('video_zoom').resetAudioDimensionToJS(newWidthVideo,newHeightVideo);
	}
}


function playVideoZoom(video)
{
	$('.wrap_video_zoom').css({zIndex: '5'});
	$('.loader_zoom_prodotti').hide();
	$(".wrapper_zoom_product").css({width: window_width+"px", height: window_height+"px"}).fadeIn();
	$(".wrapper_zoom_product").find('img.image_zoom_product').attr('src',base_url+'images/spacer.gif');
	//trackEventAnalytics('TAB_ACCESSORIES','click_accessory_<?=$this->items[0]->codAccessori?>','Tab accessory options <?=$tab->diametro?> mm');            
	var video = video;
	
	if(navigator.platform.indexOf("iPad") != -1 || navigator.platform.indexOf("iPhone") != -1)
	{
		$('#video_zoom').html('<video id="video_ipad" src="'+video+'" type="video/mp4" width="960" height="540" controls="controls" />');
		document.getElementById('video_ipad').play();	
	}
	else
	{	
		if( $('img[group="'+currentGroupImage+'"]').eq(indexImgCliccataGroup).attr('video') != "")
			setDimensionVideo(false);

		var data = new Date();
		var date = data.getHours() + ":" + data.getMinutes() + ":" + data.getSeconds() + ":" + data.getMilliseconds();
		
		if (swfobject.hasFlashPlayerVersion("10.0.0")) 
		{	
			var flashvars = {};
			flashvars.autoPlay = "1";
			flashvars.video = video;
			flashvars.loop = "0";
			flashvars.hideControls = "0";
	
			var params = {};
			params.menu = "false";
			params.quality = "high";
			params.wmode = "transparent";
			params.align = "top";
			params.allowscriptaccess = "sameDomain";
			params.allowfullscreen = "true";
			var attributes = {};
			//attributes.id = "flash_gallery_video_swf_header";
			//attributes.name = "flash_gallery_video_swf_header";	
			swfobject.embedSWF(base_url+"templates/panerai/swf/AdactoVideoPlayer.swf?d="+date,'video_zoom', newWidthVideo, newHeightVideo, "10.0.0", false, flashvars, params, attributes);
			//setScroller();
			//$('#video_manifattura').css('display','inline');
			//$('object#1').css('display','inline');
		}
	}			
	
}

function changeStatusVideo()
{
	if(videoInPlay)
	{
		videoInPlay = false;
		if($('img[group="'+currentGroupImage+'"]').eq(indexImgCliccataGroup).attr('close-after-play') != "")
			closeZoomProduct();
	}
	else
	{
		videoInPlay = true;
	}	
	
}
